!function (factory) {
    'use strict';
    'function' === typeof define && define.amd ? define(['jquery', 'ace-elements'], factory) : factory(jQuery);
}(function ($) {
    var navObj = {
        generate: function (data) {
            var result = '';
            data.forEach(function (item) {
                var itemTemplate = '<li class="">\
                                                <a href="#{{url}}" data-url="{{url}}" class="{{dropdown-toggle}}">\
                                                    <i class="menu-icon {{menu-icon}}"></i>\
                                                    <span class="menu-text"> {{label}} </span>\
                                                    {{arrow-icon}}\
                                                </a>\
                                                <b class="arrow"></b>\
                                                {{children}}\
                                            </li>';
                if (item.children !== undefined && item.children.length > 0) {
                    itemTemplate = itemTemplate
                        .replace(/{{url}}/g, '')
                        .replace('{{menu-icon}}', item.icon)
                        .replace('{{label}}', item.label)
                        .replace('{{dropdown-toggle}}', 'dropdown-toggle')
                        .replace('{{arrow-icon}}', '<b class="arrow fa fa-angle-down"></b>')
                        .replace('{{children}}', '<ul class="submenu">' + navObj.generate(item.children) + '</ul>');
                } else {
                    itemTemplate = itemTemplate
                        .replace(/{{url}}/g, item.url)
                        .replace('{{menu-icon}}', item.icon)
                        .replace('{{label}}', item.label)
                        .replace('{{dropdown-toggle}}', '')
                        .replace('{{arrow-icon}}', '')
                        .replace('{{children}}', '');
                }
                result += itemTemplate;
            });
            return result;
        },
        render: function (data) {
            var template = navObj.generate(data);
            $(this).empty();
            $(this).html(template);
        }
    };

    $.fn.ace_nav = function (params) {

        // 默认值
        var defaultValue = {
            data: null,
            type: 'get',
            async: true,
            url: null,
            success: null // 函数（参数为返回数据）
        };
        var self = this;
        var settings = $.extend({}, defaultValue, params || {});
        if (undefined !== settings.data && null !== settings.data) {
            if (settings.data === undefined) {
                console.error('data 不能为undefined');
            } else if (settings.data === null) {
                console.error('data 不能为null');
            } else {
                navObj.render.call(self, data);
            }
        } else {
            if (undefined !== settings.url && null !== settings.url && '' !== settings.url.trim()) {
                $.ajax({
                    url: settings.url,
                    type: settings.type,
                    async: settings.async,
                    success: function (result) {
                        navObj.render.call(self, result);
                        if ('function' === typeof settings.success)
                            settings.success(result);
                    }
                });
            }
        }
    };
});